<?php

class Application_Model_ApartmentMapper{

	protected $_dbTable;

	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_Apartment');
		}
		return $this->_dbTable;
	}
	
	public function save(Application_Model_Apartment $apartment){
	
		
	
		$realestateMapper = new Application_Model_RealEstateMapper();
		$row = $realestateMapper->save($apartment);
		//TODO set the object save function to return the id of the object saved
		//$where = array('model = ?' => (string)$product->getModel());
		$apartment->setRealestateId($row);
	
		$data = array(
				'id_realEstate' 	=> $apartment->getRealestateId(),
				'floor'			=> $apartment->getFloor(),
				'room'		=> $apartment->getRoom(),
				'balcony'		=> $apartment->getBalcony(),
				'garage'		=> $apartment->getGarage(),
		);
	
		if (null === ($id = $apartment->getApartmentId())) {
			unset($data['id_apartment']);
			return $this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('id_apartment = ?' => $id));
			return $id;
		}
	}
	
	public function find($id, Application_Model_Apartment $apartment)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		$apartment->setApartmentId($row->id_apartment);
		$apartment->setRealestateId($row->id_realEstate);
		$apartment->setFloor($row->floor);
		$apartment->setRoom($row->room);
		$apartment->setBalcony($row->balcony);
		$apartment->setGarage($row->garage);
	
		$realestateMapper = new Application_Model_RealEstateMapper();
		$apartment = $realestateMapper->find($apartment->getRealestateId(), $apartment);
	
		return $apartment;
	}
	
	
	public function delete($id)
	{
	
		$apartment= new Application_Model_Apartment();
		$realestateMapper = new Application_Model_RealEstateMapper();
		$apartment = $this->find($id, new Application_Model_Apartment());
	
		$result = $this->getDbTable()->find($id);
		if (0 != count($result)) {
			$this->getDbTable()->delete(array('id_apartment = ?' => $id));
		}
	
		$realestateMapper->delete($apartment->getRealestateId());
	
	
	}
	
	public function indexBuilder(Zend_Search_Lucene_Document $doc, Application_Model_Apartment $apartment)
	{
		$realestateMapper = new Application_Model_RealEstateMapper();
		$doc->addField(Zend_Search_Lucene_Field::Text('room', $apartment->getRoom(), 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::Text('floor', $apartment->getFloor(), 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::Text('garage', $apartment->getGarage(), 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::Text('balcony', $apartment->getBalcony(), 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::Text('objectType', 'apartment', 'utf-8'));
		$doc = $realestateMapper->indexBuilder($doc, $apartment);
	
		return $doc;
	}
}